package day9;

//通过Synchronized 模拟多线程 轮流并发执行
//假设是三个线程A，B，C 要求顺序是ABCABC...
//2.借助于它wait()和notify()实现
public class Demo6 implements Runnable {
    int id;
    static int count ;
    static int len;

    public Demo6(int id) {
        this.id = id;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        while (true) {
            synchronized (Demo6.class) {
                if (count % len == id) {
                    System.out.println(name);
                    count++;
                    Demo6.class.notifyAll();
                } else {
                    try {
                        Demo6.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Demo6.len =4;
        for (int i = 0; i < len; i++) {
            new Thread(new Demo6(i), (char)(65+i)+"").start();
        }
    }
}
